package org.csu.bbs.controller;

import org.csu.bbs.entity.Notice;
import org.csu.bbs.result.Result;
import org.csu.bbs.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class NoticeController {
    @Autowired
    NoticeService noticeServiceImpl;

    //根据版块返回公共
    @GetMapping(value = "api/sections/{section_id}/notice")
    @CrossOrigin
    @ResponseBody
    public List<Notice> GetNotice(@PathVariable(value = "section_id") Integer section_id){
        return noticeServiceImpl.GetBySection(section_id);
    }

    //返回所有公告
    @GetMapping(value = "api/notice")
    @CrossOrigin
    @ResponseBody
    public List<Notice> GetAllNotice(){

        return noticeServiceImpl.GetAll();
    }

    //根据公告id删除公告
    @DeleteMapping(value = "api/notice/{id}")
    @CrossOrigin
    @ResponseBody
    public int DeleteById(@PathVariable("id") Integer id){
  return noticeServiceImpl.DeleteById(id);
    }


    //发布公告
    @CrossOrigin
    @ResponseBody
    @PostMapping("/api/notice")
    public Result postNotice(@RequestBody Notice notice){
        return noticeServiceImpl.AddNotice(notice);
    }

    //编辑公告
    @CrossOrigin
    @ResponseBody
    @PutMapping("/api/notice/{id}")
    public Notice editNotice(@RequestBody Notice notice){
        return noticeServiceImpl.UpdateNotice(notice);
    }
    @CrossOrigin
    @ResponseBody
    @GetMapping("/api/notice/search")
    public List<Notice> search(@RequestParam("keywords") String keywords ){


        if ("".equals(keywords)) {
            return null;

        } else {


            return noticeServiceImpl.search(keywords);
        }
    }
    @CrossOrigin
    @GetMapping("/api/notice/content")
    @ResponseBody
    public String Content(@RequestParam( value = "id") Integer id){

        return noticeServiceImpl.GetById(id).get(0).getContent();
    }
    @CrossOrigin
    @PutMapping("/api/notice/change")
    @ResponseBody
    public void Content(@RequestBody Notice notice){

        noticeServiceImpl.changeContent(notice);
    }

}
